<?php
class dblink 
{
	private $dbh = false;
	private $res = false;
	private $host;
	private $user;
	private $pass;
	private $name;
	
	
	public function dblink($host, $user, $pass, $name) {
		$this->host = $host;
		$this->user = $user;
		$this->pass = $pass;
		$this->name = $name;
	}

	/**
	 * 返回一个记录集
	 *
	 * @param unknown_type $sql
	 * @return unknown
	 */
	public function exec($sql) {		
		if($this->connect()) {			
			mysql_query("SET NAMES 'utf8'"); 
	    	mysql_query("SET CHARACTER_SET_CLIENT=utf8"); 
	    	mysql_query("SET CHARACTER_SET_RESULTS=utf8");
			$this->res = mysql_query($sql, $this->dbh);			
		} else {			
			$this->res = false;
		}
		return $this->res;
	}
	
	/**
	 * 没有返回，比如执行delete、update
	 *
	 * @param unknown_type $sql
	 */
	public function execNonQuery($sql) {
		if($this->connect()) {
			mysql_query("SET NAMES 'utf8'"); 
	    	mysql_query("SET CHARACTER_SET_CLIENT=utf8"); 
	    	mysql_query("SET CHARACTER_SET_RESULTS=utf8");
			mysql_query($sql, $this->dbh);			
		} 
	}
			
	
	/**
	 * 没有返回的存储过程
	 *
	 * 样例：
	 * $sql = "call test(8);";
	 * $result = $dbh->execProcedureNonQuery($sql);
	 *
	 */
	public function execProcedureNonQuery($sql) {
		if($this->connect()) {
			mysql_query("SET NAMES 'utf8'");
			mysql_query("SET CHARACTER_SET_CLIENT=utf8");
			mysql_query("SET CHARACTER_SET_RESULTS=utf8");
			mysql_query($sql, $this->dbh);
		}
	}
	
	
	/**
	 * 有返回结果的存储过程
	 *
	 * 样例：
	 * $sql = "call paimai_car_over(0, @state);";
	 * $result = $dbh->execProcedureQuery($sql, "@state");
	 * print_r($result);
	 *
	 *
	 */
	public function execProcedureQuery($sql, $returnpara) {
		if($this->connect()) {
			mysql_query("SET NAMES 'utf8'");
			mysql_query("SET CHARACTER_SET_CLIENT=utf8");
			mysql_query("SET CHARACTER_SET_RESULTS=utf8");
			mysql_query($sql, $this->dbh);
			$result = mysql_query('select '.$returnpara.';');
			$array = mysql_fetch_array($result);
			return $array;
		}
	}
	
	
	public function error() {
		if($this->dbh)	return mysql_error($this->dbh);
		return "connect error";
	}
	
	public function fetch() {
		if($this->res) 	return mysql_fetch_array($this->res, MYSQL_BOTH);			
		return false;
	}
	
	public function getDataArray($res)
	{
		return mysql_fetch_array($res, MYSQL_BOTH);		
	}	
	

	/**
	 * 返回一条记录，比如查某一篇新闻
	 *
	 * @param unknown_type $sql
	 * @param unknown_type $p_clone
	 * @return unknown
	 */
	public function query($sql, $p_clone=false) {		
		if($p_clone) $dbh = $this->p_clone();
		else $dbh = &$this;
		$dbh->exec($sql);		
		return $dbh->fetch();
	}
	
	/**
	 * 获取插的记录自增长的ID值
	 *
	 * @return unknown
	 */
	public function insert_id() {	return mysql_insert_id($this->dbh); }
	
	public function p_clone() 
	{
		$mysql = new dblink($this->host, $this->user, $this->pass, $this->name);
		$mysql->dbh = $this->dbh;
		return $mysql;
	}

	public function query_1($sql, $def="", $p_clone = false) {
		//echo "<!--$sql-->";
		$row = $this->query($sql, $p_clone);
		if(is_array($row)) return $row[0];
		return $def;
	}
	
	public function num_rows() {
		if($this->res) return mysql_num_rows($this->res);	
		return 0;
	}

	public function split_page($total_row, &$cur_page, $page_size = 10) 
	{
		if($page_size < 1) $page_size = 10;

		$total_page = Tools::div($total_row + $page_size - 1, $page_size);
		
		if($total_page < 1) $total_page = 1;

		$cur_page = (int)$cur_page;
		if($cur_page <= 0 || $cur_page > $total_page) $cur_page = 1;		
		return $total_page;
	}
	
	public function list_page($sql,  &$total_row, &$cur_page, $page_size = 10, $count="*") {
		$mat = preg_split("/\sfrom\s/si", $sql);
		if(strpos(strtolower(trim($count)), "select ") === 0) {
			$total_row = $this->query_1($count, 0);
		} else {
			//$mat1 = preg_split("/\sgroup\s/si",$mat[count($mat)-1]);
			//die("SELECT COUNT($count) FROM ".$mat1[0]);			
			//$total_row = $this->query_1("SELECT COUNT($count) FROM ".$mat1[0], 0);	
			$total_row = $this->query_1("SELECT COUNT($count) FROM ($sql) tempt1");		
		}			
		
		$total_page = $this->split_page($total_row, $cur_page, $page_size);		
		
		$this->exec($sql." LIMIT ".(($cur_page-1)*$page_size)." , ".$page_size);
				
		return $total_page;
	}	
	
	public function connect() {
		if(!$this->dbh) {
			$this->dbh = mysql_connect($this->host, $this->user, $this->pass);			
			if(!$this->dbh) return false;
			mysql_select_db($this->name, $this->dbh);
		}
		
		return true;
	}
}
?>